//
//  XGL_MessageChatImageTableViewCell.swift
//  XGamesLive
//
//  Created by ios on 2024/5/28.
//

import UIKit

class XGL_MessageChatImageTableViewCell: UITableViewCell {
    var userHeader: UIImageView!
    var chatImageView: XGL_MessageChatImageView!
    
    var itemData: XGL_MessageChatData = XGL_MessageChatData(){
        didSet{
            self.userHeader.loadImageWithUrl(itemData.header)
            self.chatImageView.status = XGLMessageStatus.status(self.itemData.status)
            self.chatImageView.messageData = self.itemData
            if let coverData = self.itemData.cover,let image = UIImage(data: coverData) {
                self.chatImageView.snp.updateConstraints { make in
                    let sacle = image.size.width / image.size.height
                    if sacle > 1{
                        make.width.equalTo(ScreenWidth * 0.72)
                        make.height.equalTo(ScreenWidth * 0.72 / sacle)
                    }else{
                        make.width.equalTo(ScreenWidth * 0.36)
                        make.height.equalTo(ScreenWidth * 0.36 / sacle)
                    }
                }
            }
        }
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.contentView.backgroundColor = .clear
        self.backgroundColor = .clear
        self.selectionStyle = .none
        self.creatItemView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func creatItemView(){
        self.userHeader = UIImageView().then({ make in
            make.backgroundColor = UIColor.hexColor("#F4F8FB")
            make.layerCornerRadius = 20.0
            make.clipsToBounds = true
        })
        self.chatImageView = XGL_MessageChatImageView()
        let line = UIView()
        self.contentView.addSubviews([self.userHeader,chatImageView,line])
        userHeader.snp.makeConstraints { make in
//            make.top.equalTo(16.0)
            make.bottom.equalTo(chatImageView)
            make.right.equalTo(-16.0)
            make.width.height.equalTo(40.0)
        }
        chatImageView.snp.makeConstraints { make in
            make.right.equalTo(userHeader.snp.left).offset(-12.0)
            make.top.equalTo(16.0)
            make.width.equalTo(180)
            make.height.equalTo(180)
        }
        line.snp.makeConstraints { make in
            make.centerX.width.bottom.equalToSuperview()
            make.height.equalTo(0).priority(.high)
            make.top.equalTo(chatImageView.snp.bottom).offset(4.0)
        }
    }
}
